@extends("layout")
@section("title", "标签系统")
@section("body")
    <div class="layui-container">
        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;" id="clk-div-index-data">
            <legend>{{$title}}<i class="layui-icon layui-icon-up"></i> </legend>
        </fieldset>
        <div id="div-index-data">
            <div class="layui-row">
                <form class="layui-form" action="">
                    <div class="layui-form-item">
                        <label class="layui-form-label">标签名称:</label>
                        <div class="layui-input-inline">
                            <input type="text" class="layui-input" lay-verify="required" lay-verType="tips"
                                   name="tag_name" value="{{$old['tag_name'] or ''}}" />
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">定义者姓名:</label>
                        <div class="layui-input-inline">
                            <input type="text" class="layui-input"  lay-verType="tips"
                                   name="defined_name" value="{{$old['defined_name'] or ''}}"/>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">标签口径:</label>
                        <div class="layui-input-inline">
                            <textarea name="tag_size" class="layui-textarea">{{$old['tag_size'] or ''}}</textarea>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">标签分类:</label>
                        <div class="layui-input-inline">
                            <select name="category"  lay-verType="tips" lay-filter="filterCategory">
                                <option value="0">全部</option>
                                @foreach($data['category'] as $id => $val)
                                    <option value="{{$id}}" @if(isset($old['category']) && $old['category'] == $id)
                                    selected="selected" @endif
                                        >{{$val}}</option>
                                @endforeach
                            </select>
                        </div>
                        <div class="layui-input-inline">
                            <select name="category_child" id="category_child" lay-verType="tips"
                                    >
                                <option value="0">全部</option>
                                @foreach($data['category_child'] as $id => $val)
                                    <option value="{{$id}}" @if(isset($old['category_child']) && $old['category_child'] == $id)
                                    selected="selected" @endif>{{$val}}</option>
                                @endforeach
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">定义部门:</label>
                        <div class="layui-input-inline">
                            <select name="defined_department"  lay-verType="tips">
                                <option value="">选择</option>
                                @foreach($data['department'] as $id => $val)
                                    <option value="{{$id}}" @if(isset($old['defined_department']) && $old['defined_department'] == $id)
                                    selected="selected" @endif>{{$val}}</option>
                                @endforeach
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">标签状态:</label>
                        <div class="layui-input-inline">
                            <select name="tag_state"  lay-verType="tips">
                                <option value="">选择</option>
                                @foreach($data['tagState'] as $id => $val)
                                    <option value="{{$id}}" @if(isset($old['tag_state']) && $old['tag_state'] == $id)
                                    selected="selected" @endif>{{$val}}</option>
                                @endforeach
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">开放权限:</label>
                        <div class="layui-input-inline">
                            <select name="open_auth"  lay-verType="tips">
                                <option value="">选择</option>
                                @foreach($data['openAuth'] as $id => $val)
                                    <option value="{{$id}}" @if(isset($old['open_auth']) && $old['open_auth'] == $id)
                                    selected="selected" @endif>{{$val}}</option>
                                @endforeach
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">备注:</label>
                        <div class="layui-input-inline">
                            <input type="text" class="layui-input" name="remark" value="{{$old['remark'] or ''}}"/>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">列名:</label>
                        <div class="layui-input-inline">
                            <input type="text" class="layui-input"  lay-verType="tips"
                                   name="column_name" value="{{$old['column_name'] or ''}}"/>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">标签类型:</label>
                        <div class="layui-input-inline">
                            <select name="tag_type"  lay-verType="tips">
                                <option value="">选择</option>
                                @foreach($data['tagType'] as $id => $val)
                                    <option value="{{$id}}" @if(isset($old['tag_type']) && $old['tag_type'] == $id)
                                    selected="selected" @endif>{{$val}}</option>
                                @endforeach
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">标签精度:</label>
                        <div class="layui-input-inline">
                            <input type="text" class="layui-input"  lay-verType="tips"
                                   name="tag_precision" value="{{$old['tag_precision'] or ''}}"/>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">标签值单位:</label>
                        <div class="layui-input-inline">
                            <select name="tag_unit"  lay-verType="tips">
                                <option value="">选择</option>
                                @foreach($data['tagValueUnit'] as $id => $val)
                                    <option value="{{$id}}" @if(isset($old['tag_unit']) && $old['tag_unit'] == $id)
                                    selected="selected" @endif>{{$val}}</option>
                                @endforeach
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">标签物理类型:</label>
                        <div class="layui-input-inline">
                            <select name="tag_physics_type" lay-verType="tips">
                                <option value="">选择</option>
                                @foreach($data['tagPhysicsType'] as $id => $val)
                                    <option value="{{$id}}" @if(isset($old['tag_physics_type']) && $old['tag_physics_type'] == $id)
                                    selected="selected" @endif>{{$val}}</option>
                                @endforeach
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">表名:</label>
                        <div class="layui-input-inline">
                            <input type="text" class="layui-input" lay-verType="tips"
                                   name="table_name" value="{{$old['table_name'] or ''}}"/>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <button class="layui-btn" lay-submit lay-filter="ok" id="ok">确认</button>
                            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                            <input type="hidden" value="{{$pid}}" name="id">
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
@endsection
@section("script")
    <script>
        //一般直接写在一个js文件中
        layui.use(['layer', 'form', 'jquery', 'laydate', 'laytpl'], function () {
            var layer = layui.layer
                , form = layui.form
                , $ = layui.jquery;

            $("button[type='reset']").click(function () {
                window.location.reload();
            });
            //关闭父窗口
            function closeFather() {
                parent.location.reload();
                // var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                // parent.layer.close(index);
                // window.parent.reloadClose();
            }

            form.on('submit(ok)', function (data) {
                var params = data.field;
                $.post('{{route("add_tag")}}', params, function (rev) {
                    if(rev.status === 0) {
                        {{--layer.alert(rev.msg,{icon: 3, title:'提示'}, function () {--}}
                            {{--window.location.href = '{{route("tag")}}';--}}
                            {{--closeFather()--}}
                        {{--})--}}
                        layer.msg(rev.msg,{time:500}, function () {
                            closeFather()
                        })
                    } else {
                        layer.msg(rev.msg);
                    }
                });
                return false;
            });

            form.on('select(filterCategory)', function (data) {
                $.getJSON('{{route('change_category')}}', {id: data.value}, function (rev) {
                    if(rev.status === 0) {
                        console.log(rev.data);
                        $('#category_child').html('');
                        $("#category_child").append($("<option>").val('').text('选择'));
                        $.each(rev.data, function (key, val) {
                            var op = $("<option>").val(key).text(val);
                            $("#category_child").append(op);
                        });
                        form.render('select');
                    }
                })
            });
        });
    </script>
@endsection